/*
 * @Author: NJ_1098 xiabaoxin
 * @Date: 2025-02-11 15:10:21
 * @LastEditors: NJ_1098 xiabaoxin
 * @LastEditTime: 2025-02-14 13:46:36
 * @Description: 用户模型层
 */
const { DataTypes } = require("sequelize");
const sequelize = require("../db/sequelize");

// 创建模型(模型 UserModel -> 表 t_user)
// sequelize自动创建并管理id（主键，自增）
const UserModel = sequelize.define(
  "UserModel",
  {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      comment: "id，自增",
    },
    uid: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
      comment: "用户uid，唯一",
    },
    openid: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: true,
      comment: "微信小程序身份唯一标识",
    },
    account: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: false,
      comment: "用户登录账号",
    },
    password: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: false,
      comment: "用户登录密码",
    },
    name: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: false,
      comment: "用户名",
    },
    wname: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: false,
      comment: "微信用户名",
    },
    avatar: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "头像",
    },
    role: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: "02",
      comment: "用户角色,01-管理员,02-普通用户",
    },
    status: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: "01",
      comment: "用户状态,01-正常,02-禁用,03-关联微信后合并",
    },
    reason: {
      type: DataTypes.STRING,
      allowNull: true,
      comment: "禁用原因",
    },
  },
  {
    // 申明数据库表名，否则会按照sequelize标准
    tableName: "t_user",
    // sequelize默认使用并在表中生成createdAt和updatedAt，是否默认
    timestamps: true,
    // 将createdAt改名为create_time
    createdAt: "create_time",
    // 将updatedAt改名update_time
    updatedAt: "update_time",
  }
);

// 阻止sequelize自动生成并管理的字段（id）
// UserModel.removeAttribute("id");
// 强制同步数据库（创建数据表）
// UserModel.sync({ force: true });

module.exports = UserModel;
